<?php
namespace Admin\Controller;
use Think\Controller;
header("Content-Type:text/html; charset=utf-8");
class StudentController extends Controller {


    public function _initialize()
    {
        if(!check_login()){
            $this->error('登录状态异常,请重新登录','/Admin/Login/login');
        }
    }


    //产品列表
    public function index()
    {
        $page   =   I("get.page");
        $cate_id    =   I("get.cate_id");
        if(empty($page))    $page   =   0;
        $ids    =   D("Common")->getCateIds('pro_cate',$cate_id);
        $keywords   =   I("get.keywords");
        if($ids)    $sql    =   "cate_id in ({$ids})";
        else $sql   =   '';
        if($keywords)   {
            $sql['_string']     =   "name like '%{$keywords}%'";
        }
        $data   =   D("Common")->getList('users',$sql,'',$page,10,'id desc');


        $this->assign("lists",$data['lists']);
        $this->assign("pages",$data['pages']);
        $this->assign("cate_id",$cate_id);
        $this->assign('keywords',$keywords);
        $this->display();
    }

    public function addStudent()
    {
        if($_POST){

            $file   =   $_FILES['pic'];
            $dir='./Uploads/users/';
            $data   =   I("post.");
            if(!is_dir($dir)) {
                mkdir($dir, 0777, true);
            }
            if(!checkEmail($data['email']) && $data['email'])
                $this->error("邮箱格式错误");
            if($file['name']){
                $getPic =   upPic($file,$dir);
                if($getPic['tag']   ==  '1'){
                    $this->error($getPic['msg']);
                }else{
                    $data['pic']    =   $getPic;
                    $oPic           =   M("users")->where(array("id"=>$this->user_id))->getField("pic");
                }
            }


            $data['pwd']  =   md5('123123');
            if($data['id']){
                // 编辑
                $res    =   M("users")->where(array("id"=>$data['id']))->save($data);
            } else {
                if($data['student_id']){
                    $if_student_id = M('users')->where(array('student_id'=>$data['student_id']))->getField('id');
                    if($if_student_id) $this->error("学号已经注册过");
                }
                $res = M('users')->add($data);
            }
            if($res     !== false){
                if($oPic)   unlink('.'.$oPic);
                $this->redirect("Admin/Student/index");
            }else $this->error("编辑失败");
        }else{
            $id =   I("get.id");
            if($id){
                $page   =   I("get.page");
                $info   =   M("users")->where(array("id"=>$id))->find();

                $this->assign("info",$info);
                $this->assign("page",$page);
            }

            $this->display();
        }
    }

    public function depClass () {
        $dep_id       =   I('get.depId');
        $data   =   M("class")->where(array("dep_id"=>$dep_id))->select();
        echo json_encode($data);
    }

    public function index1()
    {
        $page       =   I('get.page');
        $cate_id    =   I("get.cate_id");
        if(empty($page))    $page   =   0;


        $ids    =   D("Common")->getCateIds('pro_cate',$cate_id);
        $keywords   =   I("get.keywords");
        if($ids)    $sql    =   "cate_id in ({$ids})";
        else $sql   =   '';
        if($keywords)   {
            $sql['_string']     =   "u.name like '%{$keywords}%'";
        }

        $pages  =   getPage('users u',$sql,$page,10,5,"ab_department as d ON d.id = u.dep_id,ab_class as c ON c.id = u.class_id");
        $num    =   $pages['page']*10;

        $lists  =   M('users u')
            ->join("ab_department as d ON d.id = u.dep_id","left")
            ->join("ab_class as c ON c.id = u.class_id","left")
            ->where($sql)->order('u.id desc')->limit($num,10)
            ->field("u.id,u.student_id,u.pic,u.name,u.phone,u.add_time,d.name depName,c.name className")
            ->select();


        $this->assign("lists",$lists);
        $this->assign("pages",$pages);
        $this->assign("cate_id",$cate_id);
        $this->assign('keywords',$keywords);
        $this->display();
    }
}